package com.aldobucchi.ds.container.utils
{
	import com.aldobucchi.core.comparison.result.ComparisonResult;
	import com.aldobucchi.core.comparison.result.ComparisonResults;
	import com.aldobucchi.core.comparison.simple.ISimpleComparable;
	
	
	
	
	/**
	 * 
	 * Properties should be read-only but we leave them public for performance
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class MultiplicityChange implements ISimpleComparable
	{
		
		
		public var oldMultiplicity:uint;
		

		public var newMultiplicity:uint;
		
		
		public var delta:int;
		


		public function get isAddition( ):Boolean
		{
			return oldMultiplicity == 0;
		}
		
		

		public function get isRemoval( ):Boolean
		{
			return newMultiplicity == 0;
		}



		
		public function MultiplicityChange( oldMultiplicity:uint, newMultiplicity:uint )
		{
		
			this.oldMultiplicity = oldMultiplicity;

			this.newMultiplicity = newMultiplicity;
		
			delta = newMultiplicity - oldMultiplicity;
		
		}
		
		
		
		// this is not being used ( apparently )
		// once we can compile the full project we should try to remove it
		public static function consolidate( multiplicityChanges:Array ):MultiplicityChange
		{

			if ( multiplicityChanges.length == 1 )
				return multiplicityChanges[ 0 ] as MultiplicityChange;
				
			if ( multiplicityChanges.length == 0 )
				return null;
				
			var oldM:int, newM:int;
			
			oldM = ( multiplicityChanges[ 0 ] as MultiplicityChange ).oldMultiplicity;
			
			newM = ( multiplicityChanges[ multiplicityChanges.length - 1 ] as MultiplicityChange ).newMultiplicity;
		
			return new MultiplicityChange( oldM, newM );
		}
		
		
		
		public function compare( other:* ):ComparisonResult
		{
			if ( this === other )
			{
				return ComparisonResults.EQUAL;
			}
		
			var o:MultiplicityChange = other as MultiplicityChange;
			
			if ( o == null )
				return ComparisonResults.DIFFERENT;
		
			
			if ( o.oldMultiplicity != this.oldMultiplicity )
				return ComparisonResults.DIFFERENT;
				
			if ( o.newMultiplicity != this.newMultiplicity )
				return ComparisonResults.DIFFERENT;
			
			
			return ComparisonResults.EQUAL;
		
		}
		
		
		
		
	}

}